package net.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.oraycn.esbasic.helpers.NumberHelper;
import com.oraycn.esbasic.helpers.StringHelper;
import com.oraycn.esframework.common.LogonResponse;
import com.oraycn.esframework.core.IRapidPassiveEngine;
import com.oraycn.esframework.core.RapidEngineFactory;

import net.example.ggtalk.R;
import net.example.utils.ConfigUtils;
import net.example.utils.ToastUtils;

import java.util.Random;

public class RegisterActivity extends Activity implements View.OnClickListener {

    private EditText et_id, et_name;
    private EditText et_password, et_password2;
    private Button btn_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ((ImageView) findViewById(R.id.p_id).findViewById(R.id.iv_ico)).setImageResource(R.drawable.phone_icon);
        ((ImageView) findViewById(R.id.p_name).findViewById(R.id.iv_ico)).setImageResource(R.drawable.nicename_icon);
        ((ImageView) findViewById(R.id.p_password).findViewById(R.id.iv_ico)).setImageResource(R.drawable.pass_icon);
        ((ImageView) findViewById(R.id.p_password2).findViewById(R.id.iv_ico)).setImageResource(R.drawable.pass_icon);

        ((TextView) findViewById(R.id.p_id).findViewById(R.id.tv_tips)).setText("帐号：");
        ((TextView) findViewById(R.id.p_name).findViewById(R.id.tv_tips)).setText("名称：");
        ((TextView) findViewById(R.id.p_password).findViewById(R.id.tv_tips)).setText("密码：");
        ((TextView) findViewById(R.id.p_password2).findViewById(R.id.tv_tips)).setText("确认密码：");

        this.et_id = (EditText) findViewById(R.id.p_id).findViewById(R.id.et_input);
//        this.et_id.setInputType(InputType.TYPE_CLASS_NUMBER);
        this.et_id.setKeyListener(DigitsKeyListener.getInstance(getResources().getString(R.string.filter_number_letter)));
        this.et_id.setFilters(new InputFilter[]{new InputFilter.LengthFilter(19)});//最多输入20位字符  因omcs 会在账号前加 2#  服务端设置的最大长度为20， 最多只能设置19
        this.et_name = (EditText) findViewById(R.id.p_name).findViewById(R.id.et_input);
        this.et_name.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});//最多输入6位字符
        this.et_password = (EditText) findViewById(R.id.p_password).findViewById(R.id.et_input);
        this.et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//设置为密码格式显示 *
        this.et_password2 = (EditText) findViewById(R.id.p_password2).findViewById(R.id.et_input);
        this.et_password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);//设置为密码格式显示 *
        this.btn_register = (Button) findViewById(R.id.btn_register);
        this.btn_register.setOnClickListener(this);
    }

    //点击注册按钮
    @Override
    public void onClick(View v) {
        try {
            this.btn_register.setEnabled(false);
            String userID = this.et_id.getText().toString().trim().toLowerCase();
            if (StringHelper.isNullOrEmpty(userID)) {
                ToastUtils.showShort(this, "帐号不能为空！");
                return;
            }
            String userName = this.et_name.getText().toString();
            if (StringHelper.isNullOrEmpty(userName)) {
                ToastUtils.showShort(this, "昵称不能为空！");
                return;
            }
            String pwd = this.et_password.getText().toString();
            if (StringHelper.isNullOrEmpty(pwd)) {
                ToastUtils.showShort(this, "密码不能为空！");
                return;
            }
            if (!this.et_password2.getText().toString().equals(this.et_password.getText().toString())) {
                ToastUtils.showShort(this, "二次密码不一致！");
                return;
            }
            Random random = new Random();
            int headIndex = random.nextInt(10);
            String token = ConfigUtils.RegistActionToken2 + String.format("%s;%s;%s;%s;%s;%d;%s;%s", userName, pwd,"","0000","#0",headIndex,"",userID);
            IRapidPassiveEngine tmp = RapidEngineFactory.CreatePassiveEngine();
            LogonResponse response = tmp.initialize("regist", token, ConfigUtils.esfServerIP, ConfigUtils.esfServerPort, null, this);
            tmp.close();
            switch (response.getFailureCause()) {
                case "Existed":
                    ToastUtils.showShort(this, "用户帐号已经存在！");
                    return;
                case "PhoneExisted":
                    ToastUtils.showShort(this, "电话号码已绑定过！");
                    return;
                case "SmsCodeExpired":
                    ToastUtils.showShort(this, "验证码不存在或已过期！");
                    return;
                case "SmsCodeError":
                    ToastUtils.showShort(this, "验证码错误！");
                    return;
                case "Error":
                    ToastUtils.showShort(this, "注册出现错误！");
                    return;
                default:
                    if (response.getFailureCause().startsWith("Succeed")) {
                        userID = response.getFailureCause().substring("Succeed".length());
                        break;
                    }
                    ToastUtils.showShort(this, response.getFailureCause());
                    return;
            }
            Intent intent = new Intent();
            intent.putExtra(getString(R.string.userID), userID);
            setResult(RESULT_OK, intent);
            finish();
        } catch (Exception e) {
            ToastUtils.showShort(this, "注册出错：" + e.getMessage());
            e.printStackTrace();
            setResult(RESULT_CANCELED);
            finish();
        }finally {
            this.btn_register.setEnabled(true);
        }
    }

    //点击返回到登录页
    public void finish(View view)
    {
        setResult(RESULT_CANCELED);
        finish();
    }

}
